package com.nari.interplatform.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPUtils {

    /**
     * 压缩(字节)
     *
     * @param srcBytes
     * @return
     * @throws Exception
     */
    public static byte[] compress(byte srcBytes[]) throws Exception {
        if (srcBytes == null || srcBytes.length == 0) {
            return null;
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        GZIPOutputStream gzipOutputStream = null;
        try {
            gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
            gzipOutputStream.write(srcBytes);
        } finally {
            try {
                if (gzipOutputStream != null) {
                    gzipOutputStream.close();
                }
            } catch (Exception e) {

            }
        }

        return byteArrayOutputStream.toByteArray();
    }

    /**
     * 压缩(字符串)
     *
     * @param srcStr
     * @return
     * @throws Exception
     */
    public static String compress(String srcStr) throws Exception {
        if (srcStr == null || srcStr.length() == 0) {
            return null;
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        GZIPOutputStream gzipOutputStream = null;
        try {
            gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
            gzipOutputStream.write(srcStr.getBytes("GBK"));
        } finally {
            try {
                if (gzipOutputStream != null) {
                    gzipOutputStream.close();
                }
            } catch (Exception e) {

            }
        }

        return Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
    }

    /**
     * 解压(字节)
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static byte[] uncompress(byte[] bytes) throws Exception {
        if (bytes == null || bytes.length == 0) {
            return null;
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        GZIPInputStream gzipInputStream = null;
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            gzipInputStream = new GZIPInputStream(in);

            byte[] buffer = new byte[2048];
            int n = 0;
            while ((n = gzipInputStream.read(buffer)) >= 0) {
                byteArrayOutputStream.write(buffer, 0, n);
            }
        } finally {
            try {
                if (gzipInputStream != null) {
                    gzipInputStream.close();
                }
            } catch (Exception e) {

            }
        }

        return byteArrayOutputStream.toByteArray();
    }

    /**
     * 解压(字符串)
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String uncompress(String str) throws Exception {
        if (str == null || str.length() == 0) {
            return null;
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        GZIPInputStream gzipInputStream = null;
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(Base64.getDecoder().decode(str));
            gzipInputStream = new GZIPInputStream(in);
            byte[] buffer = new byte[2048];
            int n = 0;
            while ((n = gzipInputStream.read(buffer)) >= 0) {
                byteArrayOutputStream.write(buffer, 0, n);
            }
        } finally {
            try {
                if (gzipInputStream != null) {
                    gzipInputStream.close();
                }
            } catch (Exception e) {

            }
        }

        return new String(byteArrayOutputStream.toByteArray(), "GBK");
    }

   /* public static void main(String[] args) throws Exception {
        String original = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><taxML xmlns=\"http://www.chinatax.gov.cn/dataspec/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><czryDm>G370784FS02</czryDm><czrymm>Jssq5678</czrymm><gwxh>0772FBC68F6D751BF7A32BDDBE768DEA</gwxh><djxh>11370700000000098005</djxh><nsrsbh>37078419860110051301</nsrsbh><nsrmc>张三</nsrmc></taxML>";
        //压缩编码
        String comStr = compress(original);
        System.out.println("压缩编码=>" + comStr);
        //解码解压
        String uncomStr = uncompress(comStr);
        System.out.println("解码解压=>" + uncomStr);

        String enCode = "H4sIAAAAAAAAC1SLsQrDIBQAfyW8PWrapQRNltApmdpCV3mRRGieQSXafn2FTh2Pu5N93l7VYXywjhQ0TEBlCN1saVHwuF/rC/SdjDpPY1VSCgrWGPeW85QSw9WSLpIt7mBIfC4QdoMcfnGbg/0b0pk5v/CTEA1/TuMNV7Pp2lKImtBAJ/Hj38O3BjpYQRiGwQB8F3yWJK1tMgiF1a6CV99ABIdQYfagj2/tZg45/F8gISWcLCPLIV/QKGxZt1LCudbFeZYVWqD392cOyGxyPHrJPrGjmHm0JqYUJ/aSplGhj+nt0TrRbwH+axBEp9BJn/VVr3NYL6BBPBI1JouksKFCf0nY775k5GW6QgEAAA==";
        System.out.println(uncompress(enCode));
    }*/
}
